const app = getApp();
const api = require("../../utils/request.js");
Page({
  data: {
    userState: {},
    userIndex: 2,
    current: 'tab1',
    id: "1",
    stateArray: [
      {
        id: "1",
        stateImage: '../../assets/images/ready-act.png',
        tip: '易孕期第一天',
        message: '',
        mainColor: '#5E93FF',
        subColor: '#DFE9FF'
      },
      {
        id: "2",
        stateImage: '../../assets/images/proceed-act.png',
        tip: '距离出生还有258天',
        message: '感谢妈妈每天为我清理脐带，辛苦了!脐带残端这几天应该就能顺利脱落了,可爱的肚脐眼就要出现了',
        mainColor: '#FF5E5E',
        subColor: '#FFDFDF'
      },
      {
        id: "3",
        stateImage: '../../assets/images/finish-act.png',
        tip: '宝宝',
        message: '感谢妈妈每天为我清理脐带，辛苦了!脐带残端这几天应该就能顺利脱落了,可爱的肚脐眼就要出现了',
        mainColor: '#5E93FF',
        subColor: '#D1F5EE'
      }
    ],
    knowledgeToday: [
      {
        "content": "伤口护理伤口护理伤口护理伤口护理",
        "coverPicture": "",
        "createTime": "",
        "id": 1,
        "linkUrl": 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
        "readCnt": 1,
        "status": "",
        "tagId": 1,
        "tagName": "月子护理",
        "title": "我是知识的标题",
        "type": "",
        "updateTime": ""
      },
      {
        "content": "伤口护理伤口护理伤口护理伤口护理",
        "coverPicture": "",
        "createTime": "",
        "id": 1,
        "linkUrl": 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
        "readCnt": 1,
        "status": "",
        "tagId": 1,
        "tagName": "月子护理",
        "title": "我是知识的标题",
        "type": "",
        "updateTime": ""
      },
      {
        "content": "伤口护理伤口护理伤口护理伤口护理",
        "coverPicture": "",
        "createTime": "",
        "id": 1,
        "linkUrl": 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
        "readCnt": 1,
        "status": "",
        "tagId": 1,
        "tagName": "月子护理",
        "title": "我是知识的标题",
        "type": "",
        "updateTime": ""
      }
    ],
    hotTopic: [
      {
        "content": "孩子沉迷网络责任最大究竟是谁",
        "coverPicture": "https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640",
        "createTime": "",
        "id": 1,
        "linkUrl": "",
        "readCnt": 1,
        "status": "",
        "tagId": 1,
        "tagName": "",
        "title": "",
        "type": "",
        "updateTime": ""
      },
      {
        "content": "奶水充足妙招",
        "coverPicture": "https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640",
        "createTime": "",
        "id": 2,
        "linkUrl": "",
        "readCnt": 1,
        "status": "",
        "tagId": 1,
        "tagName": "",
        "title": "",
        "type": "",
        "updateTime": ""
      },
      {
        "content": "孩子沉迷网络，责任最大究竟是谁",
        "coverPicture": "https://images.unsplash.com/photo-1551446591-142875a901a1?w=640",
        "createTime": "",
        "id": 3,
        "linkUrl": "",
        "readCnt": 1,
        "status": "",
        "tagId": 1,
        "tagName": "",
        "title": "",
        "type": "",
        "updateTime": ""
      }
    ],
    tagsArray: [
      {
        id: '1',
        title: '一折秒杀'
      },
      {
        id: '2',
        title: '催奶课程'
      },
      {
        id: '3',
        title: '一折秒杀'
      },
      {
        id: '4',
        title: '益生菌'
      },
      {
        id: '5',
        title: '吐奶'
      },
      {
        id: '6',
        title: '体质'
      }
    ],
    babyArray: [
      {
        id: '1',
        color: '#000',
        title: '宝宝记录'
      },
      {
        id: '2',
        color: 'blue',
        title: '喂养记录'
      },
      {
        id: '3',
        color: 'pink',
        title: '儿童成长管家'
      },
      {
        id: '4',
        color: 'lime',
        title: '心情倾诉'
      }
    ],
    pregnancyPreparationVo: {},
    pregnancyPreparationDataVo: {},
    pregnancyVo: {},
    pregnancyDataVo: {},
    apiBannerVos: [],
    // 设置状态所需参数
    params: {
      "babyBirthday": "",
      "babyGender": "1",
      "expectedDateOfChildbirth": "",
      "inoculationStatus": "0",
      "lastMensesStartDate": "",
      "mensesContinuedDays": "5",
      "mensesCycle": "",
      "openId": wx.getStorageSync('userInfo').openId
    },    
  },
  handleChange({ detail }) {
    this.setData({
      current: detail.key
    });
  },
  onReady() {
    // console.log(app.getInoculationStatus(), '    console.log(app.getInoculationStatus())')
    // if (app.getInoculationStatus()) {
    // } else {
    //   app.callback = (res) => {
    //     console.log(res, 'index.js')
    //   }
    // }
  },
  onLoad(options) {
    this.setData({
      userIndex: this.data.id - 1,
      userState: this.data.stateArray[this.data.userIndex]
    })
    // wx.setNavigationBarColor({
    //   frontColor: '#ffffff',
    //   backgroundColor: this.data.stateArray[this.data.userIndex].mainColor
    // })
    this.initData();
  },
  initData() {
    this.setData({
      id: wx.getStorageSync('userInfo').inoculationStatus,
    })    
    let param = { "openId": wx.getStorageSync('userInfo').openId }, url = "";
    if (this.data.id) {
      if (this.data.id === '1') {
        url = "/woman/api/user/pregnancyPreparationIndex"
      } else if (this.data.id === '2') {
        url = "/woman/api/user/pregnancyIndex"
      } else {
        url = "/woman/api/user/birthIndex"
        param = {
          pageNo: 1,
          pageSize: 10,
          param: { "openId": app.get_open_id() }
        }
      }
      api._post(url, param).then(res => {
        console.log(res, 'onload')
        if (res.code !== '0') {
          app.toast(res.msg)
          return false
        }
        const { hotTopic, knowledgeToday, pregnancyPreparationDataVo = {}, pregnancyPreparationVo = {}, pregnancyVo = {}, pregnancyDataVo = {}, apiBannerVos = [] } = res.data;
        this.setData({
          hotTopic,
          knowledgeToday,
          pregnancyPreparationDataVo,
          pregnancyPreparationVo,
          pregnancyVo,
          pregnancyDataVo,
          apiBannerVos
        });
        console.log(this.data.pregnancyVo)
        if (this.data.pregnancyVo.expectedDateOfChildbirth) {
          let preTime = new Date(this.data.pregnancyVo.expectedDateOfChildbirth) - new Date(this.data.pregnancyVo.lastMensesStartDate);
          const birthDay = preTime / 1000 / 60 / 60 / 24;
          console.log(birthDay, 'birthDay')
          const index = this.data.userIndex;
          const stateArray = this.data.stateArray;
          let tips = "stateArray[1].tip"
          console.log(tips)
          this.setData({
            [tips]: `距离出生11还有${birthDay}天`
          })
        }

      })
    }

  },
  // 点击修改末次月经时间
  bindDateChange(e) {
    this.setData({
      [e.target.id]: e.detail.value,
      "pregnancyPreparationVo.lastMensesStartDate": e.detail.value,
      "params.mensesCycle": 28,
      "params.mensesContinuedDays": 5,
      "params.inoculationStatus": 1,
    })
    this.setState()
  },
  // 点击怀孕
  handleBorn () {
    let that = this
    wx.showModal({
      content: "怀孕了呀~哇哦",
      success (res) {
        if (res.confirm) {
          that.setData({
            "params.inoculationStatus": 2,
            "params.mensesContinuedDays": '5',
            "params.lastMensesStartDate": that.data.pregnancyPreparationVo.lastMensesStartDate
          })
          that.calcBabyBirth()
        }
      },
    })
  },
  calcBabyBirth() {
    const lastDate = this.data.params.lastMensesStartDate //末次月经日期
    const avZhouqi = 28;//平均月经周期
    let tail = new Date(lastDate).getTime();////你的末次月经时间的毫秒表示
    //将末次月经第一天+平均月经周期天数-14天+266天
    //将末次月经第一天+平均月经周期天数+252天
    let ycq = new Date(tail + (28 + 252) * 86400000);//预产期日期
    let year = ycq.getFullYear()
    let month = this.transDate(ycq.getMonth() + 1)
    let day = this.transDate(ycq.getDate())
    this.setData({
      "params.expectedDateOfChildbirth": year + '-' + month + '-' + day
    })
    this.setState()
  },
  // 不足10补0
  transDate(num) {
    if (num > 10) {
      return num
    } else {
      return '0' + num
    }
  },
  // 状态设置
  setState () {
    api._post("/woman/api/user/setStatus", this.data.params).then(res => {
      if (res.code !== '0') {
        app.toast(res.msg)
        return false
      }
      let uinfo = wx.getStorageSync('userInfo')
      uinfo.inoculationStatus = this.data.params.inoculationStatus
      wx.setStorageSync('userInfo', uinfo)
      wx.setStorageSync('inoculationStatus', this.data.params.inoculationStatus);
    })
  },
  goMore(e) {
    const { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/article/article?id=${id}`,
    })
  }
})
